﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FYP14
{
    public partial class AddPatient : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtrecord_no.Attributes.Add("type", "number");
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    SetExpense(Convert.ToInt32(Request.QueryString["ID"]));
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Patient oPatient = new Patient();
            oPatient.Name = txtName.Text;
            oPatient.dob = Convert.ToDateTime(((TextBox)(dtcdate.FindControl("TextBox1"))).Text);
            oPatient.record_no = Convert.ToInt32(txtrecord_no.Text);
            oPatient.nic = txtnic.Text;
            oPatient.gender = txtgender.Text;
            oPatient.age = Convert.ToInt32(txtage.Text);
            oPatient.contact_number = txtcontact_number.Text;
            oPatient.address = txtaddress.Text;

            if (Request.QueryString["ID"] != null)
            {
                oPatient.ID = Convert.ToInt32(Request.QueryString["ID"]);
                BAL.UpdatePatient(oPatient);
            }
            else
            {
                BAL.SavePatient(oPatient);
            }

            Response.Redirect("ViewPatients.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewPatients.aspx");
        }

        private void SetExpense(Int32 ID)
        {
            DataTable dt = BAL.GetPatient(ID);
            if (dt.Rows.Count > 0)
            {
                txtName.Text = dt.Rows[0]["Name"].ToString();
                //txtdob.Text = dt.Rows[0]["dob"].ToString();
                txtrecord_no.Text = dt.Rows[0]["record_no"].ToString();
                txtnic.Text = dt.Rows[0]["nic"].ToString();
                txtgender.Text = dt.Rows[0]["gender"].ToString();
                txtage.Text = dt.Rows[0]["age"].ToString();
                txtcontact_number.Text = dt.Rows[0]["contact_number"].ToString();
                txtaddress.Text = dt.Rows[0]["address"].ToString();
            }
        }
    }
}